package com.gmail.hh.flickrj.sample.android.fragments;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gmail.hh.flickrj.sample.android.FlickrjMainActivity;
import com.gmail.hh.flickrj.sample.android.R;
import com.gmail.hh.flickrj.sample.android.data.DataManagerFlickr;
import com.gmail.hh.flickrj.sample.android.data.PhotoData;
import com.gmail.hh.flickrj.sample.android.data.PhotolistData;
import com.gmail.hh.flickrj.sample.android.data.PhotolistData.IPhotolistZoomChanged;
import com.gmail.hh.flickrj.sample.android.images.LazyAdapterPhotolist;
import com.gmail.hh.flickrj.sample.android.interfaces.IDataReadyListener;
import com.gmail.hh.flickrj.sample.android.interfaces.IPhotoSelectedListener;
import com.gmail.hh.flickrj.sample.android.tasks.LoadPhotostreamTask.IPhotostreamLoadedListener;
import com.googlecode.flickrjandroid.people.User;
import com.googlecode.flickrjandroid.photos.PhotoList;


@SuppressLint("NewApi")
public class FragmentHeaderUser extends Fragment implements
					IDataReadyListener{
	
	// each Activity using this fragment  must implement this interface
	public interface IRefreshHeaderUserListener {
		public void onRefreshHeaderUser();
	}
	
	private static final Logger logger = LoggerFactory.getLogger(FragmentHeaderUser.class);
	
	public static final String TAG = "fragment_headeruser";
	
	private User mUserDisplayed;
	private TextView textUserTitle;
	private TextView textUserName;
	private TextView textUserId;
	private ImageView userIcon;
	private ImageButton refreshButton;
	
	// each Activity which uses this Fragment must implement this interface
	private Activity mActivity;
	private IRefreshHeaderUserListener refreshListener;



	@Override
    public void onAttach(Activity activity) {
		this.mActivity = activity;
        super.onAttach(activity);
        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
        	refreshListener = (IRefreshHeaderUserListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement IRefreshHeaderUserListener");
        }
	}
	

	@Override
	public void onCreate(Bundle arg0) {

		super.onCreate(arg0);
		setRetainInstance(true);
	}
	

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
		// Inflate the  layout file.
		View view = inflater.inflate(R.layout.fragment_headeruser, null);
		
		this.textUserTitle = (TextView) view.findViewById(R.id.profilePageTitle);
		this.textUserName = (TextView) view.findViewById(R.id.userScreenName);
		this.textUserId = (TextView) view.findViewById(R.id.userId);
		this.userIcon = (ImageView) view.findViewById(R.id.userImage);
		this.refreshButton = (ImageButton) view.findViewById(R.id.btnRefreshUserProfile);
    	
		return view;
	}

	@Override
	public void onViewCreated(View arg0, Bundle arg1) {
		super.onViewCreated(arg0, arg1);  
		
	    
	    // reload of photos also when button clicked
		this.refreshButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				textUserName.setText("refreshClick");
				refreshListener.onRefreshHeaderUser();
			}
		});
		
	} 
	
	
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        // reload actual Data from DataManager
		if ( !hidden ) {
			boolean forceRefresh = false;
			if ( mUserDisplayed==null ) {
				forceRefresh = true;
			}
			this.reloadData(forceRefresh);
	    }
    }
    
    
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
	

    // refresh (call also be invoked by external classes)
    public void reloadData(boolean forceRefresh) {
    	if (forceRefresh)  {
    	    DataManagerFlickr.getInstance().loadUserDisplayed(this, userIcon);
        } else {
    	    onDataReady(true);
        }
    }
	
    
	// a new/actualized Data-Object is available by DataManager
	@Override
	public void onDataReady(boolean success) {
		if (success) {
			mUserDisplayed  = DataManagerFlickr.getInstance().getUserDisplayed();
			if (mUserDisplayed!=null) {
				textUserTitle.setText(mUserDisplayed.getUsername());
				textUserName.setText(mUserDisplayed.getRealName());
				textUserId.setText(mUserDisplayed.getId());
	        }
        }
	}


	

}
